<% javascript 'taxonomy_edit', 'parameter_override' %>

<%= form_for taxonomy do |f| %>
  <div class="form-group row">
    <ul class="nav nav-pills nav-stacked col-md-3" data-tabs="pills">
      <li class="active"><a href="#primary" data-toggle="tab"><%= _("Primary") %></a></li>
      <% if User.current.allowed_to?(:view_users) %>
        <li><a href="#users" data-toggle="tab"><%= _("Users") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_smart_proxies) %>
        <li><a href="#smart_proxies" data-toggle="tab"><%= _("Smart Proxies") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_subnets) %>
        <li><a href="#subnets" data-toggle="tab"><%= _("Subnets") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_compute_resources) %>
        <li><a href="#compute_resources" data-toggle="tab"><%= _("Compute Resources") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_media) %>
        <li><a href="#media" data-toggle="tab"><%= _("Media") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_provisioning_templates) %>
        <li><a href="#provisioning_templates" data-toggle="tab"><%= _("Provisioning Templates") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_ptables) %>
        <li><a href="#ptables" data-toggle="tab"><%= _("Partition Tables") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_domains) %>
        <li><a href="#domains" data-toggle="tab"><%= _("Domains") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_realms) %>
        <li><a href="#realms" data-toggle="tab"><%= _("Realms") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_hostgroups) %>
        <li><a href="#hostgroups" data-toggle="tab"><%= _("Host Groups") %></a></li>
      <% end %>
      <% if taxonomy.is_a?(Organization) && show_location_tab? %>
        <li><a href="#locations" data-toggle="tab"><%= _('Locations') %></a></li>
      <% end %>
      <% if taxonomy.is_a?(Location) && show_organization_tab? %>
        <li><a href="#organizations" data-toggle="tab"><%= _("Organizations") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_params) %>
        <li><a href="#params" data-toggle="tab"><%= _("Parameters") %></a></li>
      <% end %>
    </ul>

    <div class="tab-content stacked-content col-md-9">
      <div class="tab-pane active" id="primary">
        <%= base_errors_for taxonomy %>
        <% taxonomy_name = taxonomy.class.to_s.underscore %>
        <%= select_f(f, :parent_id, taxonomy.class.completer_scope(nil).authorized("edit_#{taxonomy_name.pluralize}").where.not(id: taxonomy.subtree_ids).order(:title), :id, :title, { :include_blank => true },
                     { :label => _('Parent'), :onchange => 'parent_taxonomy_changed(this);',
                       :help_inline => :indicator,
                       :'data-url' => (controller_name == 'organizations' ? parent_taxonomy_selected_organization_path(taxonomy.id) :
                               parent_taxonomy_selected_location_path(taxonomy.id))
                     }) if show_parent?(taxonomy) %>
        <%= text_f f, :name %>
        <%= textarea_f f, :description, :rows => 5 %>
      </div>

      <%= show_resource_if_allowed(f, taxonomy, {:resource => :users, :association => User.except_admin}) %>
      <%= show_resource_if_allowed(f, taxonomy, :smart_proxies) %>

      <%= show_resource_if_allowed(f, taxonomy, :subnets) %>
      <%= show_resource_if_allowed(f, taxonomy, :compute_resources) %>
      <%= show_resource_if_allowed(f, taxonomy, :media) %>
      <%= show_resource_if_allowed(f, taxonomy, :provisioning_templates) %>
      <%= show_resource_if_allowed(f, taxonomy, :ptables) %>
      <%= show_resource_if_allowed(f, taxonomy, :domains) %>
      <%= show_resource_if_allowed(f, taxonomy, :realms) %>

      <%= show_resource_if_allowed(f, taxonomy, :hostgroups) %>

      <% if taxonomy.is_a?(Location) && show_organization_tab? %>
        <div class="tab-pane" id="organizations">
          <%= organization_selects f, taxonomy.selected_or_inherited_ids[:organization_ids],
                                   { :disabled => taxonomy.used_and_selected_or_inherited_ids[:organization_ids], :label => _('Select organizations')},
                                   { 'data-mismatches' => taxonomy.need_to_be_selected_ids[:organization_ids].to_json,
                                     'data-inheriteds' => taxonomy.inherited_ids[:organization_ids].to_json,
                                     'data-useds' => taxonomy.used_ids[:organization_ids].to_json } %>
        </div>
      <% elsif taxonomy.is_a?(Organization) && show_location_tab? %>
        <div class="tab-pane" id="locations">
          <%= location_selects f, taxonomy.selected_or_inherited_ids[:location_ids],
                               { :disabled => taxonomy.used_and_selected_or_inherited_ids[:location_ids], :label => _('Select locations')},
                               { 'data-mismatches' => taxonomy.need_to_be_selected_ids[:location_ids].to_json,
                                 'data-inheriteds' => taxonomy.inherited_ids[:location_ids].to_json,
                                 'data-useds' => taxonomy.used_ids[:location_ids].to_json } %>
        </div>
      <% end %>

      <% if User.current.allowed_to?(:view_params) %>
        <div class="tab-pane" id="params">
          <% taxonomy_type = controller_name.singularize %>
          <% if taxonomy.parent.present? %>
              <h4><%= _('Parent parameters') %></h4>
              <%= render "common_parameters/inherited_parameters", { :inherited_parameters => taxonomy.parent_params(true),
                                                                     :parameters => taxonomy.send("#{taxonomy_type}_parameters".to_sym) } %>
          <% end %>
          <% if taxonomy.is_a?(Location) %>
              <h4><%= _("Location parameters") %></h4>
          <% else %>
              <h4><%= _("Organization parameters") %></h4>
          <% end %>
          <%= render "common_parameters/parameters", { :f => f, :type => "#{taxonomy_type}_parameters".to_sym } %>
        </div>
      <% end %>
    </div>
    <%= submit_or_cancel f %>
  </div>
<% end %>
